package com.isoftstone.storage.repository;

import java.io.Serializable;
import java.util.Properties;

import com.isoftstone.storage.StorageConstants;

/**
 * 资源仓库
 * 
 */
public class Repository implements Serializable {
	private static final long serialVersionUID = 1312227676322136247L;
	private String host;
	private int port = StorageConstants.UNKNOWN_PORT;
	private String basedir;
	private RepositoryPermissions permissions;

	/**
	 * Properties influencing storage behaviour which are very specific to
	 * particular storage.
	 */
	private Properties parameters = new Properties();

	// Username/password are sometimes encoded in the URL
	private String username = null;

	private String password = null;

	public Repository() {

	}

	/**
	 * Retrieve the base directory of the repository. This is derived from the
	 * full repository URL, and contains the entire path component.
	 * 
	 * @return the base directory
	 */
	public String getBasedir() {
		return basedir;
	}

	public void setBasedir(String basedir) {
		this.basedir = basedir;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getUrl() {

		StringBuffer sb = new StringBuffer();
		sb.append(host);

		if (port != StorageConstants.UNKNOWN_PORT) {
			sb.append(":");

			sb.append(port);
		}

		sb.append(basedir);

		return sb.toString();
	}

	public String getHost() {
		if (host == null) {
			return "localhost";
		}
		return host;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();

		sb.append("Repository[");

		sb.append(getUrl());
		sb.append("]");

		return sb.toString();
	}

	public String getParameter(String key) {
		return parameters.getProperty(key);
	}

	public void setParameters(Properties parameters) {
		this.parameters = parameters;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public RepositoryPermissions getPermissions() {
		return permissions;
	}

	public void setPermissions(RepositoryPermissions permissions) {
		this.permissions = permissions;
	}

}
